package model.semantics;

import java.util.Arrays;
import java.util.List;

import util.resources.ResourceManager;

import model.State;
import model.parser.ParserException;

/**
 * Defines a general method for combining colors together.
 * 
 * @author Robert C. Duvall and Sterling Dorminey (created templated version.)
 */
public abstract class Operation<T> {
    private int myNumArguments;
    protected String myLabel;

    /**
     * Create an operation that expects only the given number of arguments.
     */
    protected Operation(int numArguments, String label) {
        myNumArguments = numArguments;
        myLabel = label;
    }

    /**
     * Given any number of colors, combine them to create a single color.
     */
    public T evaluate(State<T> state, T... t) {
        if (t.length >= myNumArguments) {
            return evaluateSafely(state, Arrays.asList(t));
        } else {
            throw ParserException.BAD_ARGUMENTS;
        }
    }

    /**
     * Provide subclasses a safe version of evaluate.
     * 
     * This version is guaranteed to have the correct number of arguments.
     */
    protected abstract T evaluateSafely(State<T> state, List<T> colors);

    @Override
    public String toString() {
        return myLabel;
    }
}
